//
//  ClubInfoViewController.m
//  心水宝典
//
//  Created by zr on 2018/8/31.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "ClubInfoViewController.h"
#import "FunInfoModel.h"
#import "FunInfoTableViewCell.h"

@interface ClubInfoViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, weak)UIImageView * iconImageView;
@property (nonatomic, weak)UIImageView * contentImageView;
@property (nonatomic, weak)UILabel * titleLabel;
@property (nonatomic, weak)UILabel * nameLabel;
@property (nonatomic, weak)UILabel * timeLabel;
@property (nonatomic, strong)UIButton * likeButton;
@property (nonatomic, weak)UIView * lineView;
@property (nonatomic, strong)NSMutableArray <FunInfoModel *> * modelArray;
@property (nonatomic, weak)UITextField * sayTextFiled;

@end

@implementation ClubInfoViewController

- (void)viewWillAppear:(BOOL)animated{
    
//    NSLog(@"====%@",self.ID);
    NSString * url = [NSString stringWithFormat:@"/api/circle/%@",self.ID];
    
    [[NetworkTool sharedTools] GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        int code = [responseObject[@"code"] intValue];
        
        if (code == 200) {
            
            NSDictionary * dict = responseObject[@"data"];
            
            self.titleLabel.text = dict[@"depict"];
            self.nameLabel.text = dict[@"nickname"];
            self.timeLabel.text = dict[@"create_time"];
            NSURL * headUrl = [NSURL URLWithString:dict[@"head_link"]];
            [self.iconImageView sd_setImageWithURL:headUrl placeholderImage:ImageNamed(@"23sdasas")];
            NSArray * arr = dict[@"imglist_link"];
            NSURL * contentUrl = [NSURL URLWithString:arr.firstObject];
            [self.contentImageView sd_setImageWithURL:contentUrl];
            NSString * like = [NSString stringWithFormat:@"  %@",dict[@"agrees"]];
            [self.likeButton setTitle:like forState:UIControlStateNormal];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = WHITE_COLOR;
    self.modelArray = [NSMutableArray array];
    
    [self setupNav];
    [self setupUI];
}

- (void)loadData{
    
    [self.modelArray removeAllObjects];
    
    NSString * url = [NSString stringWithFormat:@"/api/comment?project_id=1&type=2&item_id=%@",self.ID];
    
    [[NetworkTool sharedTools] GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSArray *array = [NSArray yy_modelArrayWithClass:[FunInfoModel class] json:responseObject[@"data"]];
        
        [self.modelArray addObjectsFromArray:array];
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (void)setupNav{
    
    self.title = @"动态详情";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    //    backButton.frame = CGRectMake(0, 0, 20 * SCREENH6, 20 * SCREENH6);
    backButton.size = CGSizeMake(20 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
}


- (void)setupUI{
    
    UIImageView * iconImageView = [UIImageView new];
    
    iconImageView.image = ImageNamed(@"23sdasas");
    iconImageView.layer.cornerRadius = 22 * SCREENH6;
    iconImageView.layer.masksToBounds = YES;
    
    self.iconImageView = iconImageView;
    
    [self.view addSubview:self.iconImageView];
    
    UIImageView * contentImageView = [UIImageView new];
    
    contentImageView.backgroundColor = [UIColor lightGrayColor];
    
    self.contentImageView = contentImageView;
    
    [self.view addSubview:self.contentImageView];
    
    UILabel * nameLabel = [UILabel new];
    
    nameLabel.textColor = BLACK_COLOR;
    nameLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    nameLabel.text = @"神奇测试";
    
    self.nameLabel = nameLabel;
    
    [self.view addSubview:self.nameLabel];
    
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.textColor = BLACK_COLOR;
    titleLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    titleLabel.text = @"神奇测试：未来三个月，谁是你 的幸运天使。";
    
    self.titleLabel = titleLabel;
    
    [self.view addSubview:self.titleLabel];
    
    self.likeButton = [self setupButtonWithTitle:@"  2034" WithType:@"like"];
    [self.view addSubview:self.likeButton];
    
    
    UILabel * timeLabel = [UILabel new];
    
    timeLabel.textColor = [UIColor lightGrayColor];
    timeLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    timeLabel.text = @"8-22";
    
    self.timeLabel = timeLabel;
    
    [self.view addSubview:self.timeLabel];
    
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xe6e6e6);
    
    self.lineView = lineView;
    
    [self.view addSubview:self.lineView];
    
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 270 * SCREENH6, SCREEN_WIDTH, SCREENH_HEIGHT - 390 * SCREENH6) style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.rowHeight = 120 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[FunInfoTableViewCell class] forCellReuseIdentifier:@"ClubInfoTableViewCell"];
    
    [self.view addSubview:self.tableView];
    
    
    UITextField * sayTextFiled = [[UITextField alloc] init];
    
    sayTextFiled.borderStyle = UITextBorderStyleRoundedRect;
    sayTextFiled.backgroundColor = [UIColor whiteColor];
    //设置光标颜色
    sayTextFiled.tintColor = [UIColor blackColor];
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = [UIColor grayColor];
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    sayTextFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"写点什么" attributes:attrs];
    sayTextFiled.textColor = [UIColor blackColor];
    sayTextFiled.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 22 * SCREENH6, 0)];
    //设置显示模式为永远显示(默认不显示)
    sayTextFiled.leftViewMode = UITextFieldViewModeAlways;
    sayTextFiled.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    UIView * rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 35 * SCREENH6, 20 * SCREENH6)];
    UIImageView *image=[[UIImageView alloc] initWithFrame:CGRectMake(5, 0, 15 * SCREENH6, 20 * SCREENH6)];//WithImage:[UIImage imageNamed:@"手机号"]];
    image.image = ImageNamed(@"xie1");
    [rightView addSubview:image];
    sayTextFiled.leftView = rightView;
    sayTextFiled.leftViewMode = UITextFieldViewModeAlways;
    sayTextFiled.returnKeyType = UIReturnKeyDone;
    sayTextFiled.delegate = self;
    
    self.sayTextFiled = sayTextFiled;
    
    [self.view addSubview:self.sayTextFiled];
}

- (void)viewDidLayoutSubviews{
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.top.mas_equalTo(self.view).offset(10 * SCREENH6);
        make.width.height.mas_equalTo(44 * SCREENH6);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.iconImageView);
        make.leading.mas_equalTo(self.iconImageView.mas_trailing).offset(5 * SCREENH6);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.iconImageView.mas_bottom);
        make.leading.mas_equalTo(self.nameLabel);
    }];
    
    [self.contentImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.nameLabel);
        make.top.mas_equalTo(self.iconImageView.mas_bottom).offset(25 * SCREENH6);
        make.width.height.mas_equalTo(140 * SCREENH6);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentImageView);
        make.top.mas_equalTo(self.contentImageView.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.likeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.view).offset(-15 * SCREENH6);
        make.centerY.mas_equalTo(self.timeLabel);
    }];
   
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.view);
        make.height.mas_equalTo(10 * SCREENH6);
        make.top.mas_equalTo(self.likeButton.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.sayTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view).offset(-10 * SCREENH6);
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-15 * SCREENH6);
        make.height.mas_equalTo(35 * SCREENH6);
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.modelArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    FunInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ClubInfoTableViewCell" forIndexPath:indexPath];
    
    cell.model = self.modelArray[indexPath.row];
    cell.subLabel.text = [NSString stringWithFormat:@"%lu楼",indexPath.row];
    
    return cell;
    
}


- (UIButton *)setupButtonWithTitle:(NSString *)title WithType:(NSString *)type{
    
    UIButton * button = [UIButton new];
    
    NSString * image;
    if ([type isEqualToString:@"commit"]) {
        
        image = @"pinglun2";
    }else{
        
        image = @"zan";
    }
    [button setTitle:title forState:UIControlStateNormal];
    [button setImage:ImageNamed(image) forState:UIControlStateNormal];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    
    return button;
}


- (void)backToView {
    
    [self.navigationController popViewControllerAnimated:YES];
}



@end
